﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibSolPedI;
using WebLibSolPedBE;
using SAP.Middleware.Connector;


namespace LibSolPedDALC
{
    public class CLibSolPedDALC : ILibSolPed
    {

      public static CLibSolPedDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(CLibSolPedDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new CLibSolPedDALC( );
                    }
                }
            }
            return _instance;
        }

        private CLibSolPedDALC() { }
        private static volatile CLibSolPedDALC _instance = null;

        public List<SolPedBE> getLstSolPed()
        {
            List<SolPedBE> lst = new List<SolPedBE>();

            IRfcFunction function = null;
            RfcDestination dest = null;
            DestConfig dc = new DestConfig();
            try
            {
                function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_BUSCAR_SOLICITUD_PED");

                function.Invoke(DALC.rfcDest);
                IRfcTable T_RETORNO = function.GetTable("T_RETORNO");
                foreach (IRfcStructure item in T_RETORNO)
                {
                    SolPedBE it = new SolPedBE();
                    it.PREQ_NO = item["PREQ_NO"].GetString();
                    it.BNFPO = item["BNFPO"].GetString();
                    it.CENTRO = item["CENTRO"].GetString();
                    it.FECHACREA = item["FECHACREA"].GetString();
                    it.DESCMAT = item["DESCMAT"].GetString();
                    it.UNIDAD = item["UNIDAD"].GetString();
                    it.CANTIDAD = item["CANTIDAD"].GetString();
                    it.SOLICITANTE = item["SOLICITANTE"].GetString();
                    it.VALOR5 = item["VALOR5"].GetString();
                    it.CREATED_BY = item["CREATED_BY"].GetString();
                    it.CENTROCOSTO = item["CENTROCOSTO"].GetString();
                    it.FRGCO = item["FRGCO"].GetString();
                    it.VALOR1 = item["VALOR1"].GetString();
                    it.VALOR2 = item["VALOR2"].GetString();
                    it.VALOR3 = item["VALOR3"].GetString();
                    it.VALOR4 = item["VALOR4"].GetString();
                    lst.Add(it);
                }
            }
            catch(Exception e)
            {
            
            }

            return lst;
        }

        public String setLibSolPed(List<SolPedBE> lst)
        {
            String ret = "Resultado de libetación: <br>" + Environment.NewLine;
            try
            {   
                foreach (SolPedBE it in lst)
                {   
                    IRfcFunction function = null;
                    function = DALC.rfcRep.CreateFunction("ZMOVIL_MM_LIBERACION_SOLIC_PED");
                    function.SetValue("IP_PREQ_NO",it.PREQ_NO);
                    function.SetValue("IP_REL_CODE",it.FRGCO);
                    function.SetValue("IP_PREQ_ITEM",it.BNFPO);
                    function.Invoke(DALC.rfcDest);
                    IRfcTable T_RETORNO = function.GetTable("T_RETURN");
                    foreach (IRfcStructure item in T_RETORNO)
                    {
                        ret = ret + "  -" + item["MESSAGE"].GetString() + ". <br>";
                    }
                    ret = ret + "<br>" + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                ret = ret + "<br>" + "Error en el resto de documentos";
            }
            return ret;            
        }
    }
}
